package lib.servlet;

import lib.Dao.CourseDAO;
import lib.Dao.Dbutil;
import lib.Dao.UserDao;
import lib.Model.Course;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ttop5 on 16-4-22.
 */
@WebServlet(urlPatterns = "/order", name = "order")
public class Order extends HttpServlet{
    Dbutil dbutil = new Dbutil();
    UserDao userDao=new UserDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //String content = new String(req.getParameter("content").getBytes("ISO-8859-1"), "UTF-8");
        //String se=req.getParameter("se");
        List<Course> courseList=new ArrayList<>();
        String openId= (String) req.getSession().getAttribute("openid");
        String se="";
        try {
            se=userDao.getUserSe(openId);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try{
            req.getSession().removeAttribute("list");
            req.getSession().removeAttribute("result");
        }catch (Exception e){
            e.printStackTrace();
        }

        try {

            courseList=userDao.usercourse(se,openId);
            req.setAttribute("list", courseList);
            req.getRequestDispatcher("/page/order/list.jsp").forward(req, resp);
            //resp.sendRedirect("/page/dept/qrcode.jsp");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
